package cn.js.sangu.unitStore.service;

import cn.hutool.core.bean.BeanUtil;
import cn.js.sangu.unitStore.common.exception.ServiceException;
import cn.js.sangu.unitStore.dto.FileListDTO;
import cn.js.sangu.unitStore.entity.Distribute;
import cn.js.sangu.unitStore.entity.FileNas;
import cn.js.sangu.unitStore.mapper.DistributeMapper;
import cn.js.sangu.unitStore.utils.LinsUtil;
import cn.js.sangu.unitStore.utils.TokenUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 说明：工单分发服务
 * 作者：linshuo
 */
@Slf4j
@Service
public class DistributeService extends ServiceImpl<DistributeMapper, Distribute> {

    public Distribute saveInfo(Distribute dto){
        Distribute distribute = spliceAttachInfo(dto);
        distribute.setCreator(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        if (!super.save(distribute)){
            throw new ServiceException("保存失败！请联系管理员");
        }
        return distribute;
    }

    /**
     * 拼接附件信息
     * @param dto
     * @return
     */
    private Distribute spliceAttachInfo(Distribute dto){
        //临时存
        List<String> attachNameList = new LinkedList<>();
        List<Integer> ids = new LinkedList<>();
        //工单的多个附件
        List<FileListDTO> list = dto.getFileList();
        if (null != list){
            for (FileListDTO fileList : list) {
                attachNameList.add(fileList.getName());
                //取出原本的File对象
                ids.add(BeanUtil.toBean(fileList.getResponse().getData(), FileNas.class).getId());
            }
            dto.setAttachName(list.size()>0?attachNameList.toString():"");
            dto.setAttachIds(ids.toString());
            dto.setAttachCount(list.size()); //附件个数

//        dto.setFileList(dto.getFileList());
            dto.setFileListJson(new ObjectMapper().valueToTree(list));
        }
        this.keywordReplace(dto);
        return dto;
    }

    public Distribute updateInfo(Distribute dto){
        Distribute distribute = spliceAttachInfo(dto);
        //设置修改人名称
        distribute.setModifier(Objects.requireNonNull(TokenUtils.getCurrentUser()).getId());
        //补充接收信息
        addReceiveInfo(dto);
        if (!super.updateById(dto)){
            throw new ServiceException("更新失败！请联系管理员");
        }
        return distribute;
    }

    private void keywordReplace(Distribute dto){
        dto.setWorkName(LinsUtil.keywordReplace(dto.getWorkName(), null, null));
        dto.setWorkDescription(LinsUtil.keywordReplace(dto.getWorkDescription(), null, null));
    }

    private void addReceiveInfo(Distribute dto){
        if (null == dto.getReceiptStateLast()) dto.setReceiptStateLast(0);
        if (dto.getReceiptStateLast()==0 && dto.getReceiptState() == 1){
            //状态从未接收 改为 确认接收，记录接收时间
            dto.setReceiptTime(new Date());
        }
    }

}
